<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../lib/layui/css/layui.css">
    <style>
        * {
            padding: 0;
            margin: 0;
        }
    </style>
</head>

<body>

    

    <script src="../lib/layui/layui.all.js"></script>
    <script>
        /*
            如果 form 表单是动态生成的，那么它中的select、checkbox、radio是需要人为渲染的，使用 form.render() 渲染一个或所有的表单中选择器
    
            inuput输入标签可以使用lay-filter定义表单验证的值：required/phone/email/data/identity  / 自定义
    
            form.on("submit(lay-filter)", function(data){ log(data.field) })
    
            form.val("form-lay-filter", {赋值})  表单元素获取值或者赋值
        */

        var $ = layui.$;
        $('body').append(`<form class="layui-form" lay-filter="formEle">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="title" required lay-verify="title" placeholder="请输入标题" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码框</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                        autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">辅助文字</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择框</label>
                <div class="layui-input-block">
                    <select name="city" lay-verify="required" lay-filter="city">
                        <option value=""></option>
                        <option value="0">北京</option>
                        <option value="1">上海</option>
                        <option value="2">广州</option>
                        <option value="3">深圳</option>
                        <option value="4">杭州</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">复选框</label>
                <div class="layui-input-block">
                    <input type="checkbox" lay-filter='skill' name="like[write]" title="写作">
                    <input type="checkbox" lay-filter='skill' name="like[read]" title="阅读">
                    <input type="checkbox" lay-filter='skill' name="like[dai]" title="发呆">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">开关</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="switch" lay-skin="switch">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单选框</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="男" title="男">
                    <input type="radio" name="sex" value="女" title="女" checked>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">文本域</label>
                <div class="layui-input-block">
                    <textarea name="desc" lay-verify="number" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>`);

        var form = layui.form;
        console.log(form);

        // form.render(null, 'formEle')
        form.render();

        form.val('formEle', {
            'title': '舒服了',
            'password': 'dsasdasdas',
            'city': 2,
            'like[dai]': true,
            'switch': true,
            'sex': '女',
            'desc': '哈根覅欧弟和覅哈佛IHIA'
        })

        console.log('通过val取值', form.val('formEle'));

        // 自定义效验规则
        form.verify({
            // 自定义了一个名为title的校验规则
            title(value, item) { // value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                    return '用户名不能有特殊字符';
                }
                if(/(^\_)|(\__)|(\_+$)/.test(value)){
                    return '用户名首尾不能出现下划线\'_\'';
                }
                if(/^\d+\d+\d$/.test(value)){
                    return '用户名不能全为数字';
                }
            }
        })

        // form 监听事件
        // form.on("事件的名字(对应事件元素上layui-filter的值)", callback)

        form.on('select(city)', function (data) {
            console.log(data);
        })

        form.on('checkbox(skill)', function (data) {
            console.log(data);
        })
        form.on('submit(*)', function (data) {
            // 提交中的data data.field包含form表单的值 { 表单元素的name: 表单元素的值 }
            console.log(data);

            // 通过return false阻止表单跳转
            return false;
        })
    </script>
</body>

</html>